/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package perfiladoinmdm.services;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.StringTokenizer;
import perfiladoinmdm.util.Util;

/**
 *
 * @author mrequejo
 */
public class DatabaseServiceImpl implements DatabaseService {

//    private String pathCoefREE = "D:\\Mis Documentos\\Miguel Díaz\\Perfilado_09.14\\input_data\\coeficientesREE.txt";
//    private String pathMedidaAgregada = "D:\\Mis Documentos\\Miguel Díaz\\Perfilado_09.14\\input_data\\MedidaAgregada.txt";
    private String pathCoefREE = "..\\input_data\\coeficientesREE_diaGlobal.txt";
    private String pathMedidaAgregada = "..\\input_data\\MedidaAgregada.txt";

    // JDBC driver name and database URL
    static final String JDBC_DRIVER = "oracle.jdbc.OracleDriver";
    static final String DB_URL = "jdbc:oracle:thin:@172.22.247.57:1521:ENCE";

    //  Database credentials
    static final String USER = "MDM_ADMIN";
    static final String PASS = "MDM_ADMIN";    
    
    @Override
    public double[][] getCoeficientes() {
        
        return importFromDb();
    }

    public static double[][] importFromDb() {
        Connection conn = null;
        Statement stmt = null;
        
        double[][] resultado = new double [8760][9];
        
        try {
            //STEP 2: Register JDBC driver
            Class.forName(JDBC_DRIVER);

            //STEP 3: Open a connection
            System.out.println("Connecting to database...");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            //STEP 4: Execute a query
            System.out.println("Creating statement...");
            stmt = conn.createStatement();
            String sql;
            sql = "SELECT * FROM REE_COEFFICIENTS";
            ResultSet rs = stmt.executeQuery(sql);

            //STEP 5: Extract data from result set
            int i = 0;
            while (rs.next()) {
                //Retrieve by column name
                resultado[i][0] = rs.getDouble("MES");
                resultado[i][1] = rs.getDouble("DIA");
                resultado[i][2] = rs.getDouble("HORA");
                resultado[i][3] = rs.getDouble("PESO_A");
                resultado[i][4] = rs.getDouble("PESO_B");
                resultado[i][5] = rs.getDouble("PESO_C");
                resultado[i][6] = rs.getDouble("PESO_D");
                resultado[i][7] = rs.getDouble("DEMANDA_REF");
                resultado[i][8] = rs.getDouble("DIA_GLOBAL");
                i++;
                //Display values
//                for (int j = 0; i < 5; i++) {
//                    System.out.print("Fila " + i + ": ");
//                    System.out.print("" + resultado[i][0] + " ");
//                    System.out.print("" + resultado[i][1] + " ");
//                    System.out.print("" + resultado[i][2] + " ");
//                    System.out.print("" + resultado[i][3] + " ");
//                    System.out.print("" + resultado[i][4] + " ");
//                    System.out.print("" + resultado[i][5] + " ");
//                    System.out.print("" + resultado[i][6] + " ");
//                    System.out.print("" + resultado[i][7] + " ");
//                    System.out.print("" + resultado[i][8] + " ");
//                    System.out.println("");
//                    }
                
//                System.out.println("" +rs.next());
            }
            //STEP 6: Clean-up environment
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException se) {
            //Handle errors for JDBC
            se.printStackTrace();
        } catch (Exception e) {
            //Handle errors for Class.forName
            e.printStackTrace();
        } finally {
            //finally block used to close resources
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException se2) {
            }// nothing we can do
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException se) {
                se.printStackTrace();
            }//end finally try
        }//end try
        System.out.println("Goodbye!");
        return resultado;
        
    }//end main    
    
}
